﻿@using System
@using System.Linq
@using Microsoft.AspNetCore.Components
@using MudBlazor
@using MudBlazor.Docs.Models
@using Toolbelt.Blazor.HeadElement
@using MudBlazor.Docs.Extensions
@using System.Collections.Generic
@using System.Text.RegularExpressions

@*html head (for server prerendering)*@
<Title>@(GetTitle())</Title>
<Meta Name="keywords" Content="@GetKeywords()" />
<Meta Property="og:title" Content="@GetTitle()" />
<Meta Name="twitter:title" Content="@GetTitle()" />
@if (!string.IsNullOrEmpty(SubTitle))
{
    <Meta Name="description" Content="@(SubTitle)" />
    <Meta Property="og:description" Content="@SubTitle" />
    <Meta Name="twitter:description" Content="@SubTitle" />
}
@if (IsApiPage && !string.IsNullOrEmpty(ComponentLink))
{
    <Link Rel="canonical" Href="@($"https://mudblazor.com{@ComponentLink}")" />
}

<div class="docs-page-header">
    <MudText Typo="@Typo.h3">@Title</MudText>
    <MudText>@GetSubTitle() @Description</MudText>
    @if (!IsApiPage && GetComponentTypeName() != null)
    {
        <MudText Typo="@Typo.body2" Class="my-3">See <MudLink Href="@GetApiLink()">@Title API</MudLink> for parameter documentation.</MudText>
    }
    @if (IsApiPage && !string.IsNullOrEmpty(ComponentLink))
    {
        <MudText Typo="@Typo.body2">Check out the <MudLink Href="@ComponentLink">Examples</MudLink> for this component.</MudText>
    }
</div>

@code {


    [Inject] public NavigationManager NavigationManager { get; set; }

    [Parameter] public string Title { get; set; }
    [Parameter] public string SubTitle { get; set; }
    [Parameter] public string Keywords { get; set; }
    [Parameter] public RenderFragment Description { get; set; }
    [Parameter] public string ComponentLink { get; set; }


    private string GetApiLink() => $"api/{GetComponentName()}";

    private string GetComponentName()
    {
        return NavigationManager.Uri.ToString().Split('/', StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
    }

    private bool IsApiPage
    {
        get
        {
            return NavigationManager.Uri.ToString().Contains("/api/");
        }
    }

    private string GetComponentTypeName() => ApiLink.GetTypeFromComponentLink(GetComponentName())?.Name.Replace("`1", "");

    string GetTitle() => $"{Title} - MudBlazor";

    string GetSubTitle()
    {
        if (string.IsNullOrEmpty(SubTitle))
            return "";
        return SubTitle.TrimEnd('.') + ".";
    }

    string GetKeywords()
    {
        var keywords = new HashSet<string>(Regex.Split(Keywords??"", @",\s"));
        keywords.Add(Title);
        keywords.Add(GetComponentTypeName());
        keywords.Add(GetComponentTypeName()?.Replace("Mud", ""));
        keywords.Add("mudblazor");
        keywords.Add("blazor");
        keywords.Add("component");
        keywords.Add("material design");
        keywords.Remove("");
        keywords.Remove(null);
        return string.Join(", ", keywords);
    }
}